import React from 'react';
import './index.less';
import {connect} from "dva";
import Curd from "components/Curd";
import createColumns from "./columns";
import searchColumns from "./searchColumns";
import PropTypes from "prop-types";


@connect(({reward}) => ({reward}))
export default class extends Curd {

  static defaultProps = {
    align: 'center',
    searchType: 'inline',
    searchCols: {span: 8},
    showAddDelete: false,
  }

  static propTypes = {
    userId: PropTypes.number,
  };

  getNamespace() {
    return 'reward';
  }

  getTableColumns() {
    return createColumns(this);
  }

  getSearchColumns() {
    return searchColumns;
  }

  componentDidMount() {
    const {userId} = this.props;
    this.init(userId);
    this.setState({filters: {userId}});
  }

  componentDidUpdate(prevProps, prevState, snapshot) {

    if (prevProps.userId != this.props.userId) {

      const {userId} = this.props;
      this.init(userId);
      this.setState({filters: {userId}});
    }

  }

  init = (userId) => {

    let filters = {};
    if (!!userId)
      filters.userId = userId;

    this.dispatch({
      type: `${this.getNamespace()}/init`,
      payload: {
        notice: this.showMsg,
        filters
      },
    });
  }

}
